package com.cloud.salon.client.authority.repository;

import com.cloud.salon.common.base.entity.authority.Authority;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Description 权限仓库接口
 * @Author wolf--LZH
 * @CreateDate 2019/4/10 0010 下午 1:30
 */
public interface AuthorityRepository extends JpaRepository<Authority, Integer> {

    List<Authority> findByIdIn(List<Integer> ids);

    @Query(value = "SELECT a.* FROM role_authority ra "
            + "JOIN authority a ON a.id = ra.authority_id "
            + "WHERE ra.role_id = ?1", nativeQuery = true)
    List<Authority> findByRole(Integer roleId);


    @Query(value = "SELECT a.* FROM authority a "
            + "JOIN role_authority ra ON ra.authority_id = a.id "
            + "JOIN user_role ur ON ur.role_id = ra.role_id "
            + "WHERE ur.user_id = ?1 GROUP BY a.id", nativeQuery = true)
    List<Authority> findByUser(Integer userId);
}